# RedFire 交易服务 Dockerfile
# 基于现有的MainTradingEngine和OmsEngine设计
# 支持高性能订单执行和多网关路由

FROM python:3.11-slim

# 设置工作目录
WORKDIR /app

# 设置环境变量
ENV PYTHONPATH=/app
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1

# 安装系统依赖
RUN apt-get update && apt-get install -y \
    gcc \
    g++ \
    default-libmysqlclient-dev \
    pkg-config \
    curl \
    wget \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# 安装 TA-Lib 系统库
RUN cd /tmp && \
    wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \
    tar -xzf ta-lib-0.4.0-src.tar.gz && \
    cd ta-lib/ && \
    ./configure --prefix=/usr && \
    make && \
    make install && \
    cd / && \
    rm -rf /tmp/ta-lib*

# 复制依赖文件
COPY services/trading-service/requirements.txt .

# 升级pip到最新版本并安装依赖
RUN pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# 复制应用代码和共享模块
COPY services/trading-service/app/ ./app/
COPY services/trading-service/scripts/ ./scripts/
COPY shared/ ./shared/

# 创建日志目录
RUN mkdir -p /app/logs

# 创建非root用户
RUN groupadd -r trading && useradd -r -g trading trading
RUN chown -R trading:trading /app
USER trading

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
    CMD curl -f http://localhost:8002/api/v1/trading/health || exit 1

# 暴露端口
EXPOSE 8003

# 启动命令
CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8002"]
